<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Gpa_com
{
   // Constructor
   public function __construct()
   {
		if (!isset($this->CI))
		{
			$this->CI =& get_instance();
		}

        $this->CI->load->model('siam_akademik/akad_model');
   }

   function ipk($value,$rowNilai, $rowSKS)
   {
         $total_sks = 0; $total_score = 0;

	     foreach($value as $key){

            $total_sks = $total_sks + $key->$rowSKS;

               $point = $this->alphabet_to_int_score($key->$rowNilai);

               $score = $key->$rowSKS * $point;

               $total_score = $total_score + $score;

            $ipk_value = $total_score/$total_sks;

            $ipk_print = number_format($ipk_value, 2, '.', '');

         }


         return $ipk_print;
   }

   function ipk_processor()
   {
         $total_sks = 0; $total_nk = 0; $seq_number = 1;

         $data_transkrip_distinct = $this->CI->akad_model->getViewKhsAllDistinct($this->CI->session->userdata('siamunp_user'));

         if($data_transkrip_distinct)
         $data_transkrip_distinct = $this->CI->akad_model->getViewKhsAllDistinct($this->CI->session->userdata('siamunp_user'));
         else
         $data_transkrip_distinct = $this->CI->akad_model->getViewKhsAllDistinctKonversi($this->CI->session->userdata('siamunp_user'));


         foreach($data_transkrip_distinct as $transkrip_distinct){

           //$carikonversi =

           $transkrip = $this->CI->akad_model->getViewKhsAllByNim_nilaiTerbaik($this->CI->session->userdata('siamunp_user'),$transkrip_distinct->KODE_MK);

           if($transkrip){

             $total_sks = $total_sks + $transkrip->SKS;

             $nk[$seq_number] = $this->alphabet_to_int_score($transkrip->NILAI) * $transkrip->SKS;
           }
           else{

             $nk[$seq_number] = $this->alphabet_to_int_score();

           }

           $seq_number++;

         }

         for($num=1; $num < $seq_number; $num++){

           if($nk[$num]!= -1){

             $total_nk = $total_nk + $nk[$num];

           }

         }

         if($total_sks!=0)
         $nilai_ipk = $total_nk / $total_sks;
         else
         $nilai_ipk=0;

         return number_format($nilai_ipk, 2, '.', '');
	}

    function alphabet_to_int_score($input='K')  /*Nilai angka dari Nilai Huruf pada KHS */
    {
         switch($input)
         {
  	     case 'A':
	          {
	            $point=4; break;
              }
         case 'B+':
	          {
	            $point=3.5; break;
              }
         case 'B':
	          {
	            $point=3; break;
              }
	     case 'C+':
	          {
	            $point=2.5; break;
              }
         case 'C':
	          {
	            $point=2; break;
              }
	     case 'D':
	          {
	            $point=1; break;
              }
         case 'E':
	          {
	            $point=0; break;
              }
	     default :
	          {
	            $point=-1; break;
              }
         }

         return $point;
    }/* Definisikan sendiri kaidahnya. Nilai huruf dapat berkisar apa saja sesuai kebutuhan Anda. */

    function nilaikemampuan_to_nilaihuruf($nilai_angka)  /*Nilai Kemampuan berkisar antara 0 - 100 */
    {
        if($nilai_angka >= 40 && $nilai_angka <=55)
        {
          $huruf = 'D';
        }
        else if($nilai_angka >= 56 && $nilai_angka <=60)
        {
          $huruf = 'C';
        }
        else if($nilai_angka >= 61 && $nilai_angka <=70)
        {
          $huruf = 'C+';
        }
        else if($nilai_angka >= 71 && $nilai_angka <=80)
        {
          $huruf = 'B';
        }
        else if($nilai_angka >= 81 && $nilai_angka <=90)
        {
          $huruf = 'B+';
        }
        else if($nilai_angka >= 91 && $nilai_angka <=100)
        {
          $huruf = 'A';
        }
        else
        {
          $huruf = 'E';
        }

        return $huruf;
    }/* Definisikan sendiri kaidahnya. Nilai huruf dapat berkisar apa saja sesuai kebutuhan Anda. */

    function ips($value,$rowNilai, $rowSKS,$mode=NULL)
    {
         $total_sks = 0; $total_ips = 0;

	     foreach($value as $key){

            $point = $this->alphabet_to_int_score($key->$rowNilai);

            if($point!=-1)
            {

            $score = $key->$rowSKS * $point;

            }

            else

            {

            $score = 0;

            }

            $total_sks = $total_sks + $key->$rowSKS;

            $total_ips = $total_ips + $score;

            if($total_sks!=0)
            $ips_value = $total_ips/$total_sks;
            else
            $ips_value = 0;

            $ips_print = number_format($ips_value, 2, '.', '');


         }

         if($mode == 'numeric')
           $ips = $ips_value;
         else
           $ips = $ips_print;

         return $ips;
	 }

     function maxSksDiambil($value,$rowNilai, $rowSKS)
     {
         if($value !== NULL)
         {
            $ip = $this->ips($value,$rowNilai,$rowSKS,'numeric');

            if($ip>2)
	        {
	          if($ip>2.5)
	          {
		        if($ip>3.5)
		        {
			      $max=24;
		        }
		        else
		        {
			      $max=22;
		        }
	           }
	           else
	           {
	       	    $max=20;
	           }
	         }
             else $max=14;
          }
          else
             $max = 24;

          return $max;
     }

     function maxsks($ip)
     {
        if($ip>=1)
	        {
	          if($ip>=1.5)
	          {
		        if($ip>=2)
		        {
		          if($ip>=2.5)
                  {
                    if($ip>=3)
                    {
                      if($ip>=3.5)
                      {
                        $max=24;
                      }
                      else
                      {
                        $max=22;
                      }
                    }
                    else
                    {
                      $max=20;
                    }
                  }
                  else
                  {
                    $max=18;
                  }
		        }
		        else
		        {
			      $max=16;
		        }
	           }
	           else
	           {
	       	    $max=14;
	           }
	         }
             else $max=12;
          return $max;
     }

     function total($nim)
     {
         $total_sks = 0; $total_nk = 0; $seq_number = 1;

         $data_transkrip_distinct = $this->CI->akad_model->getViewKhsAllDistinct($nim);

         if($data_transkrip_distinct)
         {

         foreach($data_transkrip_distinct as $transkrip_distinct){

           $transkrip = $this->CI->akad_model->getViewKhsAllByNim_nilaiTerbaik($nim,$transkrip_distinct->KODE_MK);

           if($transkrip){

             $total_sks = $total_sks + $transkrip->SKS;

             $nk[$seq_number] = $this->alphabet_to_int_score($transkrip->NILAI) * $transkrip->SKS;
           }
           else{

             $nk[$seq_number] = $this->alphabet_to_int_score();
           }

           $seq_number++;

         }

         }

         for($num=1; $num < $seq_number; $num++){

           if($nk[$num]!= -1){

             $total_nk = $total_nk + $nk[$num];

           }

         }

        return $total_nk;

	}

    function ipk_processor_sirak($nim)
    {
         $total_sks = 0; $total_nk = 0; $seq_number = 1;

         $data_transkrip_distinct = $this->CI->akad_model->getViewKhsAllDistinct($nim);

         if($data_transkrip_distinct)
         {

         foreach($data_transkrip_distinct as $transkrip_distinct){

           $transkrip = $this->CI->akad_model->getViewKhsAllByNim_nilaiTerbaik($nim,$transkrip_distinct->KODE_MK);

           if($transkrip){

             $total_sks = $total_sks + $transkrip->SKS;

             $nk[$seq_number] = $this->alphabet_to_int_score($transkrip->NILAI) * $transkrip->SKS;
           }
           else{

             $nk[$seq_number] = $this->alphabet_to_int_score();

           }

           $seq_number++;

         }

         }

         for($num=1; $num < $seq_number; $num++){

           if($nk[$num]!= -1){

             $total_nk = $total_nk + $nk[$num];

           }

         }

         if($total_sks==0) $nilai_ipk = 0;
         else $nilai_ipk = $total_nk / $total_sks;

         return number_format($nilai_ipk, 2, '.', '');
	 }

     function inttochar($integer)
     {
       switch ($integer)
        {
          case 1: $char = 'Satu'; break;
          case 2: $char = 'Dua'; break;
          case 3: $char = 'Tiga'; break;
          case 4: $char = 'Empat'; break;
          case 5: $char = 'Lima'; break;
          case 6: $char = 'Enam'; break;
          case 7: $char = 'Tujuh'; break;
          case 8: $char = 'Delapan'; break;
          case 9: $char = 'Sembilan'; break;
          case 0: $char = 'Nol'; break;
        }
        return $char;
     }

     function ubahbilangan($bil)
     {
          $bil_asli = $bil;

        if(strlen($bil)==1)
        {
          $bil = $bil*10;
        }

        $bil = (integer)$bil;

        $temp = 0;

        for($kepalaPecahan=1;$kepalaPecahan<=9;$kepalaPecahan++)
        {
          $puluhan = $kepalaPecahan*10;
          if($bil == $puluhan)
          {
            $temp = $kepalaPecahan; break;
          }
          else if($bil > $puluhan)
          {
            $temp = $kepalaPecahan;
          }
          else
          {
            break;
          }
        }

        if($temp!=0)
        {
          $selisih = $bil - ($temp*10);
        }
        else
        {
          $selisih = $bil;
        }

        $depan = $this->inttochar($temp);
        $belakang = $this->inttochar($selisih);

        return $depan.' '.$belakang;
     }

     function inttopredikat($bil)
     {
       if($bil>2.75)
       {
         if($bil>3.5)
         $char="Dengan Pujian (<em>cumlaude</em>)";
         else
         $char="Sangat Memuaskan";
       }
       else
       $char="Memuaskan";
       return $char;
     }
}

/* Library to compute the gpa */
/* ekaprakarsa@facebook.com */